<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>包含min函数的栈</title>
</head>
<body>
  <div>
    定义栈的数据结构，请在该类型中实现一个能够得到栈的最小元素的 min 函数在该栈中，调用 min、push 及 pop 的时间复杂度都是 O(1)。
  </div>
  <script>
    /**
    * initialize your data structure here.
    */
    var MinStack = function() {
      this.stackArrA = [];
      this.stackArrB = [];
    };

    /** 
    * @param {number} x
    * @return {void}
    */
    MinStack.prototype.push = function(x) {
      this.stackArrA.push(x);
      if (!this.stackArrB.length) {
        this.stackArrB.push(x);
      } else if (x <= this.stackArrB[this.stackArrB.length - 1]) {
        this.stackArrB.push(x);
      }
    };

    /**
    * @return {void}
    */
    MinStack.prototype.pop = function() {
      let num = this.stackArrA.pop();
      num === this.stackArrB[this.stackArrB.length - 1] && this.stackArrB.pop();
      return num;
    };

    /**
    * @return {number}
    */
    MinStack.prototype.top = function() {
      return this.stackArrA[this.stackArrA.length - 1] || '';
    };

    /**
    * @return {number}
    */
    MinStack.prototype.min = function() {
      return this.stackArrB[this.stackArrB.length - 1];
    };

    /**
    * Your MinStack object will be instantiated and called as such:
    * var obj = new MinStack()
    * obj.push(x)
    * obj.pop()
    * var param_3 = obj.top()
    * var param_4 = obj.min()
    */
  </script>
</body>
</html>